import java.util.Stack;

class Solution4_2 {
    public boolean isValid(String s) {
         Stack<Character> stack = new Stack<>() ;
         int len = s.length() ;
         for(int i=0 ; i<len ; i ++){
              char ch = s.charAt(i) ;
              if(ch == '(' || ch == '{' || ch =='[' || stack.size()==0){
                stack.push(ch);
              }else{
                   char tmp = stack.peek() ;
                if(ch == ')' && tmp =='('){
                     stack.pop() ;
                }else if(ch ==']' && tmp =='['){
                    stack.pop() ;
                }else if(ch =='}' && tmp =='{'){
                    stack.pop() ;
                }else{
                    stack.push(ch) ;
                }
              }
         }
          return stack.size()==0 ; 
    }
}